<template>
  <div>
    <!-- 头部搜索 start -->
    <el-form ref="queryForm" class="query-form"
      :model="queryParams"
      size="small"
      :inline="true">
      <el-form-item class="big-query" label="关键词"
        prop="keyword">
        <el-input v-model="queryParams.keyword"
          placeholder="输入权益分类进行搜素"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
          @clear="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"
          size="small"
          @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>
    <!-- 头部搜索 end -->

    <!-- 头部操作栏 start -->

    <el-button type="primary"
      icon="el-icon-plus"
      style="margin-bottom:10px"
      size="small"
      @click="handleAdd">新增</el-button>

    <!-- 头部操作栏 end -->

    <!-- 数据表格 start -->
    <el-table v-loading="loading"
      :data="rightsList">
      <el-table-column label="分类名称"
        min-width="200"
        prop="name">
      </el-table-column>
      <el-table-column label="分类图标"
        min-width="200"
        prop="icon">
        <template slot-scope="scope">
          <el-image slot="reference"
            style="height: 36px;cursor: pointer;"
            :src="scope.row.icon"
            fit="contain"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="操作"
        fixed="right"
        class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="text"
            @click="handleUpdate(scope.row)">修改</el-button>
          <el-button
            size="small"
            type="text"
            @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.page_size"
      @pagination="getList" />
        <!-- 数据表格 end -->
      <!-- 添加或修改对话框 start -->
      <el-dialog :title="!form.id?'新增权益':'修改权益'"
        :visible.sync="open"
        width="600px"
        :close-on-click-modal="false"
        @closed="reset">
        <el-form ref="form"
          :model="form"
          :rules="rules"
          label-width="150px">
          <el-form-item label="权益分类名称" prop="name">
            <el-input v-model.trim="form.name" maxlength="15"/>
          </el-form-item>
          <el-form-item label="权益分类图标" prop="icon">
            <ImgUpload v-model="form.icon"/>
          </el-form-item>
        </el-form>
        <div slot="footer"
          class="dialog-footer">
          <el-button type="primary"
            @click="submitForm">确 认</el-button>
          <el-button @click="open = false">取 消</el-button>
        </div>
      </el-dialog>
      <!-- 添加或修改对话框 end -->
  </div>
</template>

<script>
import ImgUpload from '@/components/img-upload'
import { classList, addClass, delClass, updateClass } from '@/api/rights/class'
export default {
  name: 'Rights',
  components: {
    ImgUpload
  },
  data () {
    return {
      // 遮罩层
      loading: true,
      // 查询参数
      queryParams: {
        page: 1,
        page_size: 10,
        keyword: ''
      },
      // 总条数
      total: 0,
      // 权益表格数据
      rightsList: null,
      // 是否显示弹出层
      open: false,
      // 权益类别选项
      // 表单参数
      form: {
        name: '',
        icon: ''
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: '权益名称不能为空', trigger: 'blur' }
        ],
        icon: [
          { required: true, message: '权益分类图标不能为空', trigger: 'blur' }
        ]
      },
      iconType: 0
    }
  },
  created () {
    this.getList()
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.page = 1
      this.getList()
    },
    /** 查询权益列表 */
    getList () {
      this.loading = true
      classList(this.queryParams).then(response => {
        this.rightsList = response.data.list
        this.total = response.data.count || 0
      }).finally(() => {
        this.loading = false
      })
    },
    /** 表单重置 */
    reset () {
      this.resetForm('form')
      this.form.id = null
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.open = true
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.open = true
      this.$nextTick(() => {
        this.form.id = row.id || 11
        this.form.name = row.name
        this.form.icon = row.icon
      })
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          const params = Object.assign({}, this.form)
          if (this.form.id) {
            updateClass(params).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addClass(params).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const cat_id = row.cat_id
      const { pageIndex, pageSize } = this.queryParams
      this.$confirm(`是否确认删除该分类？分类名：${row.name}`, '警告', {
        type: 'warning'
      }).then(() => {
        delClass({ cat_id }).then(() => {
          this.queryParams.pageIndex = this.getCurrentPage({ total: this.total, pageSize, pageIndex })
          this.getList()
          this.msgSuccess('删除成功')
        })
      })
    }
  }
}
</script>
<style lang="scss">
.el-table .el-image {
  /deep/.el-image__inner {
    max-height: 36px;
  }
}
</style>
